package com.wntms.yjj.service.impl;

import com.github.pagehelper.PageHelper;
import com.wntms.yjj.dao.CustomerManagerDao;
import com.wntms.yjj.pojo.Customer;
import com.wntms.yjj.service.CustomerManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class CostomerManagerServiceImpl implements CustomerManagerService {

    @Autowired
    private CustomerManagerDao customerDao;

    @Override
    public boolean delete(Integer id) {
        System.out.println(id);
        Customer customer = customerDao.findById(id);
        customerDao.deleteC(id);
        Integer count = customerDao.deleteU(customer.getUid());
        return count < 1 ? false : true;
    }

    @Override
    public boolean update(Customer customer) {
        Integer uid = customerDao.findById(customer.getId()).getUid();
        System.out.println(uid);
        String name = customer.getName();
        customerDao.updateC(customer);
        Integer count = customerDao.updateU(uid, name);
        return count < 1 ? false : true;
    }

    @Override
    public List<Customer> findAll() {
        List<Customer> list = customerDao.findAll();
        return list;
    }

    @Override
    public Customer findByTel(String tel) {
        Customer customer = customerDao.findByTel(tel);
        return customer;
    }

    @Override
    public Customer findByName(String name) {
        Customer customer = customerDao.findByName(name);
        return customer;
    }

    @Override
    public List<Customer> findByType(String type) {
        List<Customer> list = customerDao.findByType(type);
        return list;
    }

    @Override
    public Customer findByCondition(Customer customer) {
        Customer returnCustomer=null;
        if (customer.getName().length()!=0){
             returnCustomer=this.findByName(customer.getName());
             return returnCustomer;
        }

        if(customer.getTel().length()!=0){
            System.out.println(customer.getTel());
           return returnCustomer=this.findByTel(customer.getTel());
        }

        return returnCustomer;
    }

    @Override
    public List<Customer> findByPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Customer> list = customerDao.findAll();
        return list;

    }
}
